Skip to content

fix: reject aborted requests with AbortSignal.reason#1886

Open
TrevorBurnham wants to merge 1 commit intosmithy-lang:mainfrom
TrevorBurnham:fix/abort-signal-reason
Open

fix: reject aborted requests with AbortSignal.reason#1886
TrevorBurnham wants to merge 1 commit intosmithy-lang:mainfrom
TrevorBurnham:fix/abort-signal-reason

Conversation

@TrevorBurnham
Copy link
Contributor

Issue #, if available:

Fixes #1360

Description of changes:

When a request is aborted, all three HTTP handlers (NodeHttpHandler, NodeHttp2Handler, FetchHttpHandler) reject with a generic new Error("Request aborted"). This makes it impossible for callers to determine which AbortSignal caused the abort, especially when using AbortSignal.any().

The fix adds a buildAbortError helper to each handler that uses AbortSignal.reason when available:

  • If reason is an Error, it is used directly (preserving the caller's error type and stack).
  • If reason is a non-Error value (e.g. a string), it is wrapped in an Error with name: "AbortError".
  • If no reason is set (older runtimes or bare { aborted: true } signals), the existing "Request aborted" behavior is preserved.
// Before — always a generic error
const abortError = new Error("Request aborted");
abortError.name = "AbortError";
reject(abortError);

// After — uses the signal's reason when available
const abortError = buildAbortError(abortSignal);
reject(abortError);

All six abort sites across the three handlers were updated (two per handler: the early-abort check and the onAbort listener).

Tests added to node-http-handler.spec.ts:

  • Rejects with the custom Error reason directly
  • Falls back to default "Request aborted" when no reason is set
  • Wraps non-Error string reasons in an Error with name: "AbortError"

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

When a request is aborted, the http handlers now use the AbortSignal's
reason property (if available) instead of always creating a generic
Error('Request aborted'). This lets callers using AbortSignal.any()
identify which signal caused the abort.

If reason is an Error instance, it is used directly. If it is a
non-Error value (e.g. a string), it is wrapped in an Error with
name 'AbortError'. If no reason is set, the existing behavior is
preserved with the default 'Request aborted' message.

Applied to all three handlers:
- NodeHttpHandler (http/https)
- NodeHttp2Handler (http2)
- FetchHttpHandler (fetch)

Fixes smithy-lang#1360
@TrevorBurnham TrevorBurnham requested a review from a team as a code owner February 21, 2026 21:27
/**
* Builds an abort error, using the AbortSignal's reason if available.
*/
function buildAbortError(abortSignal?: { reason?: unknown }): Error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should appear only once in this package

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Aborted requests should be rejected with AbortSignal.reason

2 participants